const hireDao = require('../dao/hireDao');

const Response = require('../entity/http/Response');

const hireService = {
    /**
     * 获取所有岗位类型接口
     */
    async getAllJobTypes() {
        let result;
        try {
            result = await hireDao.getAllJobTypes();
            result = (new Response()).ok("操作成功", result);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * 创建一个组织
     */
    async createOrganization(uid, organization) {
        let result;
        try {
            result = await hireDao.createOrganization(uid, organization);
            result = (new Response()).ok("操作成功", result.insertId);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * 创建一个岗位
     */
    async publishJob(uid, job) {
        let result;
        try {
            result = await hireDao.publishJob(uid, job);
            if (result.insertId) {
                result = (new Response()).ok("操作成功", result.insertId);
            } else {
                result = (new Response()).fail(400, "操作失败");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * 懒加载推荐岗位接口
     */
    async getRecommendJobs(uid, lastId) {
        let result;
        try {
            result = await hireDao.getRecommendJobs(uid, lastId);
            for (let i = 0; i < result.length; i++) {
                let logoId = result[i].logoId;
                let url = await hireDao.getLogoUrl(logoId);
                result[i].logoUrl = url;
            }
            result = (new Response()).ok("操作成功", result);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    async getJobInfo(uid, id) {
        let result;
        try {
            result = await hireDao.getJobInfo(uid, id);
            if (result.length === 0) {
                result = (new Response()).fail(400, "岗位不存在");
            } else {
                result = result[0];
                let logoId = result.logoId;
                let url = await hireDao.getLogoUrl(logoId);
                result.logoUrl = url;
                result = (new Response()).ok("操作成功", result);
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    async collectJob(uid, id, collect) {
        let result;
        try {
            result = await hireDao.collectJob(uid, id, collect);
            if (result.affectedRows) {
                result = (new Response()).ok("操作成功");
            } else {
                result = (new Response()).fail(500, "操作失败");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    async submit(uid, id) {
        let result;
        try {
            result = await hireDao.submit(uid, id);
            if (result.affectedRows) {
                result = (new Response()).ok("操作成功");
            } else {
                result = (new Response()).fail(500, "操作失败");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    }
};

module.exports = hireService;